<template>
  <div>
    <h1>头像修改</h1>
    <div class="PictureShow">
      <el-upload
        class="upload-demo"
        drag
        :action="uploadAction"
        :on-success="uploadSuccess"
        :before-upload="beforeUpload"
        :data="userInfo"
        multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将头像拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg文件，且不超过2MB</div>
      </el-upload>
    </div>
  </div>
</template>

<script>
export default {
  name: 'UserPicture',
  data () {
    return {
      userInfo: {
        uid: window.sessionStorage.getItem('uid')
      },
      uploadAction: this.$axios.defaults.baseURL + 'user/upload'
    }
  },
  methods: {
    uploadSuccess (res) {
      window.sessionStorage.setItem('icon', res.data.icon)
    },
    beforeUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 10

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style scoped>
.avatar-uploader el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
h1{
  text-align: center;
}
.PictureShow{
  width: 1000px;
  margin-left: auto;
  margin-right: auto;
}
</style>
